package com.kscrm.web.controller.web;

import java.util.List;
import java.util.Objects;
import javax.validation.Valid;

import com.kscrm.common.utils.SecurityUtils;
import com.kscrm.web.domain.*;
import com.kscrm.web.dto.AddActivityClientDto;
import com.kscrm.web.dto.AddActivityExpertDto;
import com.kscrm.web.service.IActivityClientService;
import com.kscrm.web.service.IActivityExpertService;
import com.kscrm.web.service.IClientExpertService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.kscrm.common.annotation.Log;
import com.kscrm.common.core.controller.BaseController;
import com.kscrm.common.core.domain.AjaxResult;
import com.kscrm.common.enums.BusinessType;
import com.kscrm.web.service.IActivityService;
import com.kscrm.common.core.page.TableDataInfo;

/**
 * 活动Controller
 * 
 * @author Dhq
 * @date 2025-05-12
 */
@RestController
@RequestMapping("/web/activity")
public class ActivityController extends BaseController
{
    @Autowired
    private IActivityService activityService;

    @Autowired
    private IActivityExpertService activityExpertService;

    @Autowired
    private IActivityClientService activityClientService;
    @Autowired
    private IClientExpertService clientExpertService;

    /**
     * 查询活动列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Activity activity)
    {
        startPage();
        Long channelId = SecurityUtils.getChannelId();
        if (channelId!= null) {
            activity.setChannelId(channelId);
        }
        List<Activity> list = activityService.selectActivityList(activity);
        return getDataTable(list);
    }

    /**
     * 获取活动详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(activityService.selectActivityById(id));
    }


    /**
     * 活动专家
     * @return
     */
    @PutMapping(value = "editExpert")
    public AjaxResult editExpert(@RequestBody @Valid AddActivityExpertDto dto){
        activityExpertService.deleteActivityExpertByActivityId(dto.getActivityId());
        for (Long expertId : dto.getExpertIdList()){
            activityExpertService.insertActivityExpert(new ActivityExpert().setActivityId(dto.getActivityId()).setExpertId(expertId));
        }
        return success();
    }

    /**
     * 活动客户
     * @return
     */
    @PutMapping(value = "editClient")
    public AjaxResult editClient(@RequestBody @Valid AddActivityClientDto dto){
        activityClientService.deleteActivityClientByActivityId(dto.getActivityId());
        for (Long clientId : dto.getClientIdList()){
           // 新增客户
            activityClientService.insertActivityClient(new ActivityClient().setClientId(clientId).setActivityId(dto.getActivityId()));
            // 新增绑定
            Activity activity = activityService.selectActivityById(dto.getActivityId());
            if(!Objects.isNull(activity) && !activity.getExpertList().isEmpty()){
                for (Expert expert : activity.getExpertList()){
                    clientExpertService.insertClientExpert(
                            new ClientExpert()
                                    .setClientId(clientId)
                                    .setExpertId(expert.getId()));
                }
            }
        }
        return success();
    }


    /**
     * 新增活动
     */
    @Log(title = "新增活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Activity activity)
    {
        return toAjax(activityService.insertActivity(activity));
    }

    /**
     * 修改活动
     */
    @Log(title = "编辑活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Activity activity)
    {
        return toAjax(activityService.updateActivity(activity));
    }

    /**
     * 删除活动
     */
    @Log(title = "活动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(activityService.deleteActivityByIds(ids));
    }
}
